# -*- coding: utf-8 -*-

#This file is part of pyLot library.
#
# pyLot is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pyLot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with pyLot.  If not, see <http://www.gnu.org/licenses/>.

__author__ = u'Guillaume Baty'
__copyright__ = u"Copyright 2011-2012 (C) andheo, Université de Pau et des Pays de l'Adour"
__credits__ = [u'Guillaume Baty']
__license__ = "GNU Lesser General Public License"

__all__ = [
  'CliModifierPyLot',
  'CliModifierApplication',
  'CliModifierInformation'
  ]

from traits.api import implements

from _pylot.cli.interfaces.i_climodifier import ICli, ICliSimpleModifier

class CliModifierPyLot(object):

  implements(ICli)

  def __init__(self, parser):
    self.parser = parser
    group = self.parser.add_argument_group('pyLot Debug Options')
    group.add_argument("--check-interfaces",
                             help="Check interface implementations",
                             action="store_true")
    group.add_argument("--qt-api", action="store", default='pyside')
    group.add_argument("--qt-api-version", action="store", type=str, default='0')
    group.add_argument("--app-identifier", action="store", type=str)


    self.console = False


  def parse(self, namespace):
    u"""
    This method do not need target.
    All can be done here!
    """
    self.app_identifier = namespace.app_identifier
    if namespace.check_interfaces :
      import traits.has_traits
      traits.has_traits.CHECK_INTERFACES = 2
    import os
    os.environ['QT_API'] = namespace.qt_api
    os.environ['QT_API_VERSION'] = namespace.qt_api_version

class CliModifierApplication(object):
  implements(ICliSimpleModifier)

  def __init__(self, parser):
    self.parser = parser
#    group = self.parser.add_argument_group('application debug options')

    group = self.parser #.add_argument_group('Common Options')
    group.add_argument("-c", "--console", help="Do not launch GUI",
                       action="store_true", dest="console")
    group.add_argument("--verbose", help="Verbose mode", action="store_true")
    group.add_argument("-i", "--input", help="Input file", default='OUTPUT',
                       action="store", metavar='INPUT')
    group.add_argument("-o", "--output", help="Output file", default='OUTPUT',
                       action="store", metavar='OUTPUT')
    group.add_argument("--output-dir", help="Output directory", default='OUTPUT',
                       action="store", metavar='OUTPUT')
    group.add_argument("--dry-run", help="Do not launch commands",
                      action="store_true")

    group.add_argument("--force-ui-generation",
                       help="Force qt designer file generation",
                       action="store_true")
    group.add_argument("--debug-plugins",
                       help="Stop program on plugin errors",
                       action="store_true")

  def checkTarget(self, target):
    from pyLot.core import APPLICATION
    assert(target == APPLICATION)

  def parseTarget(self, namespace, target):
    u"""
    This method is called before target instantiation.
    All work done here will be available at target instantiation (__init__)
    target is a class
    """
    self.console = namespace.console
    if target is None :
      from pyLot.application import APPLICATION
      target = APPLICATION
    self.checkTarget(target)
    target.FORCE_UI_GENERATION = namespace.force_ui_generation
    target.DEBUG_PLUGINS = namespace.debug_plugins

class CliModifierInformation(object):
  implements(ICliSimpleModifier)

  def __init__(self, parser):
    self.parser = parser
    group = self.parser.add_argument_group('application debug options')
    group.add_argument("--info-plugins",
                       help="Show plugins informations",
                       action="store_true")

  def checkTarget(self, target):
    from pyLot.core import APPLICATION
    assert(target == APPLICATION)

  def parseTarget(self, namespace, target=None):
    u"""
    This method is called after target instantiation.
    target is an instance
    """
    if namespace.info_plugins :
      target.displayExtensionInformations()



if __name__ == '__main__' :
  pass
